<template>
  <div class="box home">
    <div class="my">
      <div class="my-l">
        <van-uploader :max-size="600 * 600" :max-count="1" :after-read="readOk" @oversize="onOversize">
        <van-image
          width="100"
          height="100"
          :src="avatar"
        />
        </van-uploader>
        <span>Hello,{{userMsg.userName}}</span>
      </div>
      <div class="my-r">
        <span class="iconfont icon-kefu" @click="$router.push('/login')"></span>
        <span class="iconfont icon-lingdang" @click="$router.push('/login')"></span>
      </div>
    </div>
    <van-notice-bar class="vip"
      left-icon="volume-o"
      text="限时7.5折开会员，领66元专享红包 。"
/>
<van-notice-bar mode="link"><van-icon name="vip-card-o" right-icon="arrow"/> 天 天 都 领 0 元 菜 , 1 人 开 会 员 全 家 可 共 享</van-notice-bar>
  <van-grid>
  <van-grid-item icon="photo-o" text="余额">
    <span>￥100</span>
    <p>余额</p>
  </van-grid-item>
  <van-grid-item icon="photo-o" text="余额">
    <span>1</span>
    <p>优惠券</p>
  </van-grid-item>
  <van-grid-item icon="photo-o" text="余额">
    <span>1</span>
    <p>鲜币</p>
  </van-grid-item>
  <van-grid-item icon="photo-o" text="余额">
    <span>1</span>
    <p>积分兑换</p>
  </van-grid-item>
</van-grid>
<div class="pink">
  <div class="title"               @click="$router.push('/order')">
    <h3>我的订单</h3>
    <p>全部订单 ></p>
  </div>
 <van-grid :column-num="5">
  <van-grid-item v-for="val in proList" :key="val.title" @click="$router.push('/order')">
  <span :class="['iconfont',val.icon]"></span>
  <p>{{val.title}}</p>
  </van-grid-item>
</van-grid>
</div>
<div class="blue">
  <div class="title">
    <h3>我的服务</h3>
  </div>
 <van-grid :column-num="4">
  <van-grid-item v-for="val in serverList" :key="val.title" @click="onService(val.title)">
  <span :class="['iconfont',val.icon]"></span>
  <p>{{val.title}}</p>
  </van-grid-item>
</van-grid>
</div>
</div>
</template>
<script>
import Vue from 'vue'
import { setUserAvatar, getUserinfo, exit } from '@/api/request.js'
import { NavBar, Icon, NoticeBar, Grid, GridItem, Toast, Uploader, Image as VanImage } from 'vant'

Vue.use(NavBar)
Vue.use(Icon)
Vue.use(NoticeBar)
Vue.use(Grid)
Vue.use(GridItem)
Vue.use(Toast)
Vue.use(Uploader)
Vue.use(VanImage)
export default {
  beforeRouteEnter (to, from, next) {
    if (sessionStorage.getItem('isLogin') === 'ok') {
      next()
    } else {
      next('/login')
    }
  },
  data () {
    return {
      proList: [{
        title: '待支付',
        icon: 'icon-daizhifu'
      },
      {
        title: '待配送',
        icon: 'icon-xiangzi'
      },
      {
        title: '配送中',
        icon: 'icon-truck'
      },
      {
        title: '待评价',
        icon: 'icon-icon_fuben'
      },
      {
        title: '售后',
        icon: 'icon-shouhou'
      }],
      serverList: [{
        title: '收获地址',
        icon: 'icon-dizhi'
      }, {
        title: '客服和帮助',
        icon: 'icon-service'
      }, {
        title: '退出登录',
        icon: 'icon-shezhi'
      }, {
        title: '推荐有礼',
        icon: 'icon-richscan_icon'
      }, {
        title: '邀请得40元',
        icon: 'icon-hongbao'
      }, {
        title: '0元免费拿',
        icon: 'icon-mianfei'
      }, {
        title: '积分商城',
        icon: 'icon-jifen'
      }, {
        title: '限时领免邮券',
        icon: 'icon-icon'
      }, {
        title: '储值送红包',
        icon: 'icon-chuzhi'
      }],
      userMsg: {},
      avatar: ''
    }
  },
  mounted () {
    getUserinfo({
      id: sessionStorage.getItem('userId')
    }).then(data => {
      this.avatar = data.data.msg.avatar
      this.userMsg = data.data.msg
      console.log(this.userMsg)
    })
  },
  methods: {
    onOversize (f) {
      console.log(f)
      Toast('请上传 600 x 600 像素的图片')
    },
    readOk (f) {
      console.log(f.content)
      setUserAvatar({
        id: sessionStorage.getItem('userId'),
        avatar: f.content
      }).then(data => {
        console.log(data.data)
        this.avatar = data.data.msg
      })
    },
    onService (t) {
      if (t === '退出登录') {
        console.log(444)
      }
      exit({
        id: sessionStorage.getItem('userId')
      }).then(data => {
        if (data.data.code === 200) {
          sessionStorage.setItem('userId', '')
          sessionStorage.setItem('isLogin', '')
          sessionStorage.setItem('token', '')
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>
<style lang="scss">
.my{
  padding: 0.2rem 0.1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .my-l .my-r{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .my-l{
    display: flex;
    justify-content: space-between;
    align-items: center;
    i{
      font-size: 0.5rem
    }
    span{
      margin-left: 0.1rem;
    }
  }
  .my-r{
    span{
      margin-left: 0.1rem;
      font-size: 0.3rem;
    }
  }
}
.vip {
color:rgba(0, 0, 0, 0.973);
}
.title{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.2rem 0.1rem;
}
.pink{
.van-grid-item{
  span{
  color: hotpink;
  font-size: 0.2rem;
  }
}
}
.blue{
  span{
    font-size: 0.3rem;
  }
  p{
    font-size: 0.12rem;
  }
}
</style>
